<?php

/**
 * Subclass for representing a row from the 'geo_name' table.
 *
 * 
 *
 * @package lib.model
 */ 
class GeoName extends BaseGeoName
{
    /**
     * Stores GeoName objects from DB
     */
    protected static $m_arGeoObjs = array();
    
    
    /**
     * Returns array of GeoName object by country code. Array is sorted by priority and name
     *
     * @param string $countryCode country code - LT, US, RU
     * @return array of GeoName
     */
    public static function getCitiesByCountry($countryCode)
    {
    	$arCities = array();
    	
    	if( $countryCode )
    	{
    	    $c = new Criteria();
    	    $c->add(GeoNamePeer::COUNTRY_CODE, $countryCode);
    	    $c->addDescendingOrderByColumn(GeoNamePeer::PRIORITY);
    	    $c->addAscendingOrderByColumn(GeoNamePeer::NAME);
    	    
    	    $arCities = GeoNamePeer::doSelect($c);
    	}
    	
    	return $arCities;
    }
    
    /**
     * Retrieves (cached) GeoName object from DB by PK or empty GeoName object
     *
     * @param integer $id ID in table `geo_name`
     * @return GeoName
     */
    public static function getById($id) 
    {
        // If object is not retrieved
        if( empty( self::$m_arGeoObjs[$id] ) )
        {
            // Ensure we got object
            $obj = GeoNamePeer::retrieveByPK($id);
            if( !$obj )
            {
                $obj = new GeoName();
            }
        }else 
        {
            $obj = self::$m_arGeoObjs[$id];
        }
        
        return $obj;
    }
    
    /**
     * Returns latitude by GeoName object ID
     *
     * @param integer $id ID in `geo_name`
     * @return float
     */
    public static function getLatitudeById($id) 
    {
	   $obj = GeoName::getById($id);
	   return $obj->getLatitude();
    }
    
    /**
     * Returns longitude by GeoName object ID
     *
     * @param integer $id ID in `geo_name`
     * @return float
     */
    public static function getLongitudeById($id) 
    {
    	$obj = GeoName::getById($id);
    	return $obj->getLongitude();
    }
    
    /**
     * Returns name of status
     *
     * @return string
     */
    public function getStatusName()
    {
        return MyConstants::getStatusName($this->status_id);
    }

    public function __toString()
    {
    	return $this->name;
    }
    
    
}
